package com.ftg.learn.code.controller;

import com.ftg.learn.code.servcie.CodeService;
import com.ftg.learn.common.constant.CodeConstant;
import com.ftg.learn.common.dto.LoginByPhoneDto;
import com.ftg.learn.common.result.Result;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.ftg.learn.common.constant.CodeConstant.OK;
import static com.ftg.learn.common.constant.MessageErrorConstant.CODE_ERROR;
import static com.ftg.learn.common.constant.MessageErrorConstant.PHONE_ERROR;

@Slf4j
@RestController
@RequestMapping("/code")
@Tag(name = "验证码接口")
public class CodeController {

    @Autowired
    private CodeService codeService;

    @GetMapping
    @Operation(summary = "根据电话号码生成验证码")
    public Result<String> generateCode(String phone) {
        log.info("生成验证码:{}", phone);
        return codeService.getCode(phone);
    }

    @PostMapping
    @Operation(summary = "验证验证码")
    public Result<Boolean> verifyCode(@RequestBody LoginByPhoneDto loginByPhoneDto) {
        log.info("验证码验证:{}", loginByPhoneDto);
        boolean isValid = codeService.validateCode(loginByPhoneDto);
        if (!isValid) {
            return Result.build(OK, CODE_ERROR,  false);
        }
        return Result.build(OK, "验证码验证成功", true);
    }
}

